package com.tevintao.marketingsystem.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.tevintao.marketingsystem.R;
import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.AccountManager;
import com.tevintao.marketingsystem.tool.SystemPathTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class ActSetGps extends AppCompatActivity implements View.OnClickListener {

    private EditText mLatEditText, mLonEditText, mLacEditText, mMncEditText, mCidEditText;
    private Button mSetBtn;

    public static final String KEY_LAT = "lat";
    public static final String KEY_LON = "lon";
    public static final String KEY_LAC = "lac";
    public static final String KEY_MNC = "mnc";
    public static final String KEY_CID = "cid";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_set_gps);
        mLatEditText = (EditText) findViewById(R.id.act_gps_lat);
        mLonEditText = (EditText) findViewById(R.id.act_gps_lon);
        mMncEditText = (EditText) findViewById(R.id.act_gps_mnc);
        mLacEditText = (EditText) findViewById(R.id.act_gps_lac);
        mCidEditText = (EditText) findViewById(R.id.act_gps_cid);
        mSetBtn = (Button) findViewById(R.id.act_gps_set);
        mSetBtn.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new Thread(mResetLocation).start();
    }

    private Runnable mResetLocation = new Runnable() {
        @Override
        public void run() {
            if (SystemPathTool.exitFile(AccountManager.LOCATION_FILE)) {
                String json = SystemPathTool.getOutPutContent(AccountManager.LOCATION_FILE);
                if (TextUtils.isEmpty(json)) {
                    LogManager.getInstance().logError("ActSetGps json is null");
                }
                String decode = null;
                try {
                    decode = URLDecoder.decode(json, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                if (TextUtils.isEmpty(decode)) {
                } else {
                    updateLocationMap(decode);
                    LogManager.getInstance().logInfo("当前位置信息 = " + decode);
                }
            }
        }
    };

    private boolean updateLocationMap(String json) {
        LogManager.getInstance().logError("ActSetGps updateHookJson json = " + json);
        if (TextUtils.isEmpty(json)) {
            return false;
        }
        try {
            final JSONObject jo = new JSONObject(json);
//            Iterator<String> keys = jo.keys();
//            while (keys.hasNext()) {
//                String key = keys.next();
//                mInfoMap.put(key, jo.getString(key));
//            }
            ActSetGps.this.getWindow().getDecorView().post(new Runnable() {
                @Override
                public void run() {
                    try {
                        if (jo.has(KEY_LAT)) {
                            mLatEditText.setText(jo.getString(KEY_LAT));
                        }

                        if (jo.has(KEY_LON)) {
                            mLonEditText.setText(jo.getString(KEY_LON));
                        }

                        if (jo.has(KEY_LAC)) {
                            mLacEditText.setText(jo.getString(KEY_LAC));
                        }


                        if (jo.has(KEY_MNC)) {
                            mMncEditText.setText(jo.getString(KEY_MNC));
                        }

                        if (jo.has(KEY_CID)) {
                            mCidEditText.setText(jo.getString(KEY_CID));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        LogManager.getInstance().logError("ActSetGps JSONException " + e);
                    }
                }
            });
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            LogManager.getInstance().logError("ActSetGps updateHookJson" + e.getMessage());
            return false;
        }
    }

    @Override
    public void onClick(View v) {
        if (v == mSetBtn) {
            final JSONObject jo = new JSONObject();
            float lat = Float.parseFloat(mLatEditText.getText().toString());
            float lon = Float.parseFloat(mLonEditText.getText().toString());
            int lac = Integer.parseInt(mLacEditText.getText().toString());
            int mnc = Integer.parseInt(mMncEditText.getText().toString());
            int cid = Integer.parseInt(mCidEditText.getText().toString());

            try {
                jo.put(KEY_LAT, lat);
                jo.put(KEY_LON, lon);
                jo.put(KEY_LAC, lac);
                jo.put(KEY_MNC, mnc);
                jo.put(KEY_CID, cid);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String location = jo.toString();
                    final boolean writeSuccess = AccountManager.writeLocation(location);
                    ActSetGps.this.getWindow().getDecorView().post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(ActSetGps.this, "写入位置信息 = " + writeSuccess + "  " + jo.toString(), Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }).start();



        }
    }
}
